package com.mallcai.bigdata.ladon.datasource;

import com.cake.bigdata.starter.exception.category.BusinessException;
import com.mallcai.bigdata.ladon.dpl.vo.DataType;
import com.mallcai.bigdata.ladon.utils.CollectionUtils;
import com.mallcai.bigdata.ladon.utils.DateUtils;
import com.mallcai.bigdata.ladon.vo.DataVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.ResultSetExtractor;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.*;

/**
 * @author feixy
 * @version 1.0
 * @date 2019-07-16 19:30
 */
@Slf4j
public class DataVoExtractor implements ResultSetExtractor<DataVo> {

    private final Map<String, DataType> displayDataTypes = new HashMap<>();

    public DataVoExtractor() {
        this(null);
    }


    public DataVoExtractor(Map<String, String> displayDataTypes) {
        if(CollectionUtils.isNotEmpty(displayDataTypes)){
            displayDataTypes.forEach((key, value)-> this.displayDataTypes.put(key, DataType.getByCode(value)));
        }
    }


    @Override
    public DataVo extractData(ResultSet resultSet) {
        try{
            DataVo dataVo = new DataVo();
            //获取列表对象
            ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
            String column;
            //获取查询结果字段信息
            final int size = resultSetMetaData.getColumnCount();
            List<String> columns = new ArrayList<>();
            Map<String, DataType> columnTypes = new HashMap<>();
            for (int i = 1; i <= size; i++) {
                column = resultSetMetaData.getColumnLabel(i);
                DataType type = ResultSetDataTool.getDataType(resultSetMetaData.getColumnClassName(i));
                columnTypes.put(column, type);
                columns.add(column);
            }

            dataVo.setColumns(columns);         //添加返回对象中

            //获取结果集
            Object columnValue;
            List<Map<String, Object>> rows = new ArrayList<>();
            while (resultSet.next()) {
                Map<String, Object> row = new HashMap<>();
                for (int i = 1; i <= size; i++) {
                    column = resultSetMetaData.getColumnLabel(i);
                    DataType dt = columnTypes.get(column);
                    if (dt == null) {
                        continue;
                    }
                    columnValue = ResultSetDataTool.getValue(resultSet, column, dt);

                    row.put(column, convertValue(columnValue,this.displayDataTypes.get(column), dt));
                }
                rows.add(row);
            }
            log.debug("查询结果: 数据{}条", rows.size());
            if(CollectionUtils.isNotEmpty(this.displayDataTypes)){
                dataVo.setColumnTypes(displayDataTypes);
            }
            dataVo.setColumnTypes(columnTypes); //添加字段数据类型
            dataVo.setRows(rows);
            dataVo.setRowSize(rows.size());
            return dataVo;
        }catch (Exception e){
            throw new BusinessException(e);
        }
    }

    public static Object convertValue( Object columnValue,DataType dataType, DataType originalDateType) {
        Object result = columnValue;
        if(originalDateType == DataType.LONG || originalDateType == DataType.INTEGER ){
            if(dataType == DataType.DATETIME){
                result = DateUtils.formatDate(new Date((Long)columnValue), "yyyy-MM-dd HH:mm:ss");
            } else if (dataType == DataType.DATE){
                result = DateUtils.formatDate(new Date((Long)columnValue), "yyyy-MM-dd");
            } else if (dataType == DataType.TIME){
                result = DateUtils.formatDate(new Date((Long)columnValue), "HH:mm:ss");
            }
        }
        return result;
    }

    public static void main(String[] args) {
        System.out.println(new Date(1559739616000L));
    }
}
